<?php
class graphics {
        function graphics() {

        }

        function generateValidationImage($rand,$tip) {
		
			if($tip==1){
                $width = 80;
                $height = 40;
				$image= imagecreatefromjpeg("captcha2.jpg");
                $bgColor = imagecolorallocate ($image, 255, 255, 255);
                //$textColor = imagecolorallocatealpha ($image,220,194,195,0);
				$textColor = imagecolorallocatealpha ($image,255,255,255,0);
				$textColor2 = imagecolorallocatealpha ($image,220,194,175,0);
				
				//$textColorbb = imagecolorallocatealpha ($image,216,33,20,0);
				$textColorbb = imagecolorallocatealpha ($image,255,47,140,0);
				// $textColor1 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
				// $textColor2 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
				// $textColor3 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
				// $textColor4 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
				// $textColor5 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
                //imagestring($image, 5, 13,4 , $rand, $textColor);
				$_l1 = substr($rand,0,1);
				$_l2 = substr($rand,1,1);
				$_l3 = substr($rand,2,1);
				$_l4 = substr($rand,3,1);
				$_l5 = substr($rand,4,1);
				imagestring($image, 4, 2,4 ,'A B 4 5 6', $textColorbb);
				imagestring($image, 4, 2,8 ,'A D 0 5 6', $textColorbb);
				imagestring($image, 4, 2,14 ,'6 D 8 5 6', $textColorbb);
				$_posh = array(2,6,10,6,2);
				$idx = mt_rand(0,4);
				imagestring($image, 4, 18,$_posh[$idx] , $_l1, $textColor);
					//imagestring($image, 4, 0,$_posh[$idx],'$', $textColor2);
				$idx = mt_rand(0,4);
				imagestring($image, 4, 28,$_posh[$idx] , $_l2, $textColor);
				$idx = mt_rand(0,4);
				imagestring($image, 4, 34,$_posh[$idx] , $_l3, $textColor);
				$idx = mt_rand(0,4);
				imagestring($image, 4, 46,$_posh[$idx] , $_l4, $textColor);
				$idx = mt_rand(0,4);
				imagestring($image, 4, 56,$_posh[$idx] , $_l5, $textColor);
					//imagestring($image, 4, 68,$_posh[$idx]-5 , '&', $textColor);
				$grey = ImageColorAllocate($image, 255,178,177); 
				$grey1 = ImageColorAllocate($image, 152,62,61); 
				 //imageline($image, 0, $height/2, $width-3, $height/2, $grey); 
				 imageline($image, 0, ($height/2)-5, $width-3, ($height/2)-5, $grey); 
				 //imageline($image, 0, ($height/2)-15, $width-3, ($height/2)-15, $grey); 
				 //imageline($image, 0, ($height/2)-10, $width-3, ($height/2)-12, $grey); 
				//imagestring($image, 5, 17,8 , $rand, $textColor);
                header("Expires: Mon, 23 Jul 1993 05:00:00 GMT");
                header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
                header("Cache-Control: no-store, no-cache, must-revalidate");
                header("Cache-Control: post-check=0, pre-check=0", false);
                header("Pragma: no-cache");
                header('Content-type: image/jpeg');
                imagejpeg($image);
                imagedestroy($image);
			}

			if($tip==2){
				
				$_mpng1 = mt_rand(1,10);
				$_mpng2 = mt_rand(1,10);
				while($_mpng2==$_mpng1){
					$_mpng2 = mt_rand(1,10);
				}
				$_mpng3 = mt_rand(1,10);
				while($_mpng3==$_mpng1){
					$_mpng3 = mt_rand(1,10);
				}

				
				$_png1 = imagecreatefrompng($_mpng1.'.png');
				$_png2 = imagecreatefrompng($_mpng2.'.png');
				$_png3 = imagecreatefrompng($_mpng3.'.png');
				
				$_imagen = imagecreatefromjpeg('captcha2.jpg');

				$_Xp = imagesx($_imagen); 
				$_Yp = imagesy($_imagen); 

				$_Xs1 = imagesx($_png1); 
				$_Ys1 = imagesy($_png1); 

				$_l1 = mt_rand(4,10);
				$_l2 = mt_rand(3,10);
				$_l3 = mt_rand(5,8);
				$_l4 = mt_rand(1,10);
				$_l5 = mt_rand(0,5);				
				
				switch($rand){
				case 3 :
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1, $_Yp-$_Ys1-$_l1, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 17, $_Yp-$_Ys1-$_l2, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png3, $_Xp-$_Xs1 - 34, $_Yp-$_Ys1-$_l3, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 49, $_Yp-$_Ys1-$_l4, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png2, $_Xp-$_Xs1 - 64, $_Yp-$_Ys1-$_l5, 0,0, $_Xs1, $_Ys1 );
					break;
				case 4 :
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1, $_Yp-$_Ys1-$_l1, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 17, $_Yp-$_Ys1-$_l2, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png3, $_Xp-$_Xs1 - 34, $_Yp-$_Ys1-$_l3, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 49, $_Yp-$_Ys1-$_l4, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 64, $_Yp-$_Ys1-$_l5, 0,0, $_Xs1, $_Ys1 );
					break;					
				case 5 :
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1, $_Yp-$_Ys1-$_l1, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 17, $_Yp-$_Ys1-$_l2, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 34, $_Yp-$_Ys1-$_l3, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 49, $_Yp-$_Ys1-$_l4, 0,0, $_Xs1, $_Ys1 );
					imagecopy( $_imagen, $_png1, $_Xp-$_Xs1 - 64, $_Yp-$_Ys1-$_l5, 0,0, $_Xs1, $_Ys1 );
					break;					
				}				
				imagejpeg($_imagen,$path,100);
				imagedestroy($image);		
			}
			
			if($tip==3){
                $width = 80;
                $height = 40;
				$image= imagecreatefromjpeg("captcha2.jpg");
                $bgColor = imagecolorallocate ($image, 255, 255, 255);
                //$textColor = imagecolorallocatealpha ($image,220,194,195,0);
				$textColor = imagecolorallocatealpha ($image,255,255,255,0);
				$textColor2 = imagecolorallocatealpha ($image,220,194,175,0);
				
				//$textColorbb = imagecolorallocatealpha ($image,216,33,20,0);
				$textColorbb = imagecolorallocatealpha ($image,255,47,140,0);
				// $textColor1 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
				// $textColor2 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
				// $textColor3 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
				// $textColor4 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
				// $textColor5 = imagecolorallocatealpha ($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),0);
                //imagestring($image, 5, 13,4 , $rand, $textColor);
				if($rand >= 15){
					$_op = ' + ';
					$_item1 = mt_rand(0,15);
					$_item2 = $rand - $_item1;
				}else{
					$_op = ' - ';
					$_item1 = mt_rand(16,30);
					$_item2 = $_item1 - $rand;
				}
				//$rand = $_item1.$_op.$_item2;
				
				$_l1 = $_item1;
				$_l3 = $_op;
				$_l5 = $_item2;

				imagestring($image, 4, 2,4 ,'A B 4 5 6', $textColorbb);
				imagestring($image, 4, 2,8 ,'A D 0 5 6', $textColorbb);
				imagestring($image, 4, 2,14 ,'6 D 8 5 6', $textColorbb);
				$_posh = array(2,6,10,6,2);
				$idx = mt_rand(0,4);
				imagestring($image, 5, 18,$_posh[$idx] , $_l1, $textColor);
					//imagestring($image, 4, 0,$_posh[$idx],'$', $textColor2);
					//$idx = mt_rand(0,4);
					//imagestring($image, 4, 28,$_posh[$idx] , $_l2, $textColor);
				$idx = mt_rand(0,4);
				imagestring($image, 5, 34,$_posh[$idx] , $_l3, $textColor);
					//$idx = mt_rand(0,4);
					//imagestring($image, 4, 46,$_posh[$idx] , $_l4, $textColor);
				$idx = mt_rand(0,4);
				imagestring($image, 5, 56,$_posh[$idx] , $_l5, $textColor);
					//imagestring($image, 4, 68,$_posh[$idx]-5 , '&', $textColor);
				$grey = ImageColorAllocate($image, 255,178,177); 
				$grey1 = ImageColorAllocate($image, 152,62,61); 
				 //imageline($image, 0, $height/2, $width-3, $height/2, $grey); 
				 //imageline($image, 0, ($height/2)-5, $width-3, ($height/2)-5, $grey); 
				 //imageline($image, 0, ($height/2)-15, $width-3, ($height/2)-15, $grey); 
				 //imageline($image, 0, ($height/2)-10, $width-3, ($height/2)-12, $grey); 
				//imagestring($image, 5, 17,8 , $rand, $textColor);
                header("Expires: Mon, 23 Jul 1993 05:00:00 GMT");
                header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
                header("Cache-Control: no-store, no-cache, must-revalidate");
                header("Cache-Control: post-check=0, pre-check=0", false);
                header("Pragma: no-cache");
                header('Content-type: image/jpeg');
                imagejpeg($image);
                imagedestroy($image);			
			}	
        }
}
?>